package com.itbird.eventbus;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.itbird.eventbus.annotation.Subscribe;
import com.itbird.eventbus.annotation.ThreadMode;
import com.itbird.eventbus.core.EventBus;

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 注册事件
         */
        EventBus.getIntance().register(this);

        textView = findViewById(R.id.textview);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN_THREAD)
    public void getMsg(EventMsg msg) {
        if (msg != null && !TextUtils.isEmpty(msg.getMsg())) {
            textView.setText(msg.getMsg());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getIntance().unregister(this);
    }
}